/* *****************************************************************************
 *
 *	danizone.ch
 *
 *	Developer:		mda010
 *	Creation date:	May 6, 2012 10:09:17 PM
 *	
 *	Project name:	HomegrownCuisine
 *	File name:		AdminController.java
 *
 *	Copyrights (C) 2012 @danizone.ch
 *
 *   This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *	GNU General Public License for more details.
 *
 *	Change history:	[developer]; [date];[reason for change]
 *
 *					mda010; May 6, 2012; created
 *
 ***************************************************************************** */
package ch.zhaw.hgc.controllers;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import ch.zhaw.hgc.entities.Recipe;
import ch.zhaw.hgc.entities.Season;
import ch.zhaw.hgc.entities.UserAccount;
import ch.zhaw.hgc.services.ISeasonService;
import ch.zhaw.hgc.services.IUserAccountService;

@Controller
@RequestMapping("/login")
public class LoginController {

	private static final Logger logger = Logger
			.getLogger(LoginController.class);

	private List<UserAccount> userAccounts;

	@Autowired
	private IUserAccountService userAccountService;

	/**
	 * @param userAccount
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView get(UserAccount userAccount) {

		ModelAndView model = new ModelAndView("login");

		model.addObject("userAccounts", userAccountService.findAll());

		model.addObject("userAccount", new UserAccount());
		model.addObject("message", "");

		logger.info("Passed get() method in controller...");

		return model;
	}

	/**
	 * @param userAccount
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView submitLogin(
			@ModelAttribute("userAccount") UserAccount userAccount) {
		ModelAndView model = new ModelAndView("login");
		// userAccountService.save(userAccount);

		userAccounts = userAccountService.findAll();

		boolean loginOK = false;
		for (int i = 0; i < userAccounts.size(); i++) {

			if (userAccounts.get(i).getValidationValue() != null) //Not registered!
				if (userAccounts.get(i).getValidationValue().equals(""))// Not validated
					// Name ok?
					if (userAccounts.get(i).getAccountName()
							.equals(userAccount.getAccountName())) {
						// Password ok?
						if (userAccounts.get(i).getPasscode()
								.equals(userAccount.getPasscode())) {

							loginOK = true;
							break;
						}
					}

		}

		if (loginOK) {

			model.addObject("message", "Successfully logged in");
			return new ModelAndView("redirect:/admin");



		} else {

			model.addObject("message", "Bad login!!!");
			return model;

		}

//		logger.info("Passed get() method in controller...");

	

	}

}